﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Shopping
{
    public class Shopping:IShopping
    {
        public void AddShopping(Model.Shopping shopping, ref Model.NutriLeafDBEntities context)
        {
            context.Shoppings.AddObject(shopping);
            context.SaveChanges();
        }

        public void RemoveShopping(int id, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Shoppings.Where(p => p.ShoppingID == id).First());
            context.SaveChanges();
        }

        public void RemoveShopping(Model.Shopping shopping, ref Model.NutriLeafDBEntities context)
        {
           context.DeleteObject(context.Shoppings.Where(p=>p.ShoppingID==shopping.ShoppingID).First());
           
        }

        public Model.Shopping GetShopping(int id, ref Model.NutriLeafDBEntities context)
        {
            return context.Shoppings.Where(p => p.ShoppingID == id).First();
        }

        public void Update(Model.Shopping shopping, ref Model.NutriLeafDBEntities context)
        {
            Model.Shopping s = context.Shoppings.Where(p => p.ShoppingID == shopping.ShoppingID).First();
            s = shopping;
            context.SaveChanges();
        }

        public List<Model.Shopping> GetShoppingEventsByUser(int userid, int top, ref Model.NutriLeafDBEntities context)
        {
            return context.Shoppings.Where(p => p.UserID == userid).Take(top).ToList();

        }

        public Model.Shopping GetRecentShoppingEvent(int userId, ref Model.NutriLeafDBEntities context)
        {
          return   context.Shoppings.Where(p => p.UserID == userId).First();

        }
    }
}